//
//  HMVisitorView.swift
//  Black22
//
//  Created by Apple on 16/11/20.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  定义代理协议
protocol HMVisitorViewDelegate: NSObjectProtocol {
    //  登录操作
    func loginOperation()
}


class HMVisitorView: UIView {

    //  定义代理对象
    weak var delegate: HMVisitorViewDelegate?
    //  设置需要执行的闭包
    var callBack: (()->())?
    
    // MARK:    --懒加载
    fileprivate lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    fileprivate lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    fileprivate lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        //  多行显示
        label.numberOfLines = 0
        //  文字居中
        label.textAlignment = .center
        return label
    }()
    
    fileprivate lazy var registerButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: #selector(registerAction), for: .touchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: .stretch), for: .normal)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        return button
    }()
    
    fileprivate lazy var loginButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: .stretch), for: .normal)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        return button
    }()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        //  设置背景色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(iconImageView)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(iconImageView)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
        
    }
    
    //  开启旋转动画
    fileprivate func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  动画目的地
        animation.toValue = 2 * M_PI
        //  动画时长
        animation.duration = 20
        //  动画执行次数
        animation.repeatCount = MAXFLOAT
        //  执行动画
        cycleImageView.layer.add(animation, forKey: nil)
        
        
    }
    
    //  更新访客视图信息
    func updateVisitorInfo(message: String?, imageName: String?) {
        
        if let msg = message, let imgName = imageName {
            //  设置不同的访客内容
            messageLabel.text = msg
            //  设置不同的图片
            iconImageView.image = UIImage(named: imgName)
            //  隐藏旋转图片
            cycleImageView.isHidden = true
        } else {
            //  首页访客信息
            startAnimation()
            
        }
        
        
    }
    
    
    // MARK:    --点击事件
    @objc private func registerAction() {
        print("注册")
        delegate?.loginOperation()
        //  执行闭包
        callBack?()
    }
    
    @objc private func loginAction() {
        print("登录")
        delegate?.loginOperation()
        //  执行闭包
        callBack?()
    }
    
   

}

extension HMVisitorView {
    func test() {
        //
        addSubview(iconImageView)
        //  使用手写代码自动布局
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
    }

}

